package com.qk.exception;

import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalException {
  /*  @ExceptionHandler
    public Result handle(Exception e) {
        log.error("服务器异常：{}", e.getMessage());
        return Result.error("您的网络有问题,请稍后再试");
    }*/
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException  e){
        String emsg = e.getMessage();
        if (emsg.contains("Duplicate entry")){
            String msg = emsg.substring(emsg.indexOf("Duplicate entry"));
            String[] arr = msg.split(" ");
            return Result.error(arr[2] + " 已存在");
        }
        return Result.error("服务器异常");
    }
    @ExceptionHandler
    public Result handleBusinessException(BusinessException e){
        log.error("业务异常：{}", e.getMessage());
        return Result.error(e.getMessage());
    }
}
